package com.cxs.xiaomi.web.controller;

import com.cxs.xiaomi.domain.Goods;
import com.cxs.xiaomi.service.impl.GoodsServiceImpl;
import com.cxs.xiaomi.utils.FileUploadUtil;
import com.cxs.xiaomi.utils.StringUtil;
import cxs.utils.utils.ParamsAsBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;


/*
 * @Author:cxs
 * @Date:2021/9/14  15:51
 * @Version 1.0
 * @Motto:放下杂念,只为迎接明天更好的自己
 * */
@WebServlet(name = "GoodsFileServlet", urlPatterns = "/addGoods")
@MultipartConfig(maxFileSize = 1024 * 1024 * 4, maxRequestSize = 1024 * 1024 * 20)
public class GoodsFileServlet extends HttpServlet {
    private static final String ALLOW_UPLOAD_TYPE = "jpg,png";

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("method");
        String basePath = request.getServletContext().getRealPath("WEB-INF" + File.separator + "images");
        if (StringUtil.empty(method)) {
            request.setAttribute("msg", "操作有误");
            request.getRequestDispatcher("/message.jsp").forward(request, response);
            return;
        }
        if ("add".equals(method.trim())) {
            try {
                Goods goods = ParamsAsBean.parammeterAsBean(Goods.class, request, true);
                Part picture = request.getPart("picture");
                if (picture != null) {
                    String filename = picture.getSubmittedFileName();
                    if(!"".equals(filename)){
                        String post = filename.substring(filename.lastIndexOf(".") + 1);
                        String[] allFilePost = ALLOW_UPLOAD_TYPE.split(",");
                        List<String> list = Arrays.asList(allFilePost);
                        if (!list.contains(post)) {
                            request.setAttribute("msg", "上传类型不匹配");
                            request.getRequestDispatcher("/message.jsp").forward(request, response);
                            return;
                        }
                    }
                }
                String fileRealPath = FileUploadUtil.upload(picture, basePath);
                if (fileRealPath != null && fileRealPath.length() > 0) goods.setPicture(fileRealPath);
                else goods.setPicture(null);

                if (new GoodsServiceImpl().addGoods(goods)) {
                    response.sendRedirect(request.getContextPath() + "/goodsservlet?method=getGoods");
                } else {
                    request.setAttribute("msg", "添加失败，请重新尝试");
                    request.getRequestDispatcher("/admin/addGoods.jsp").forward(request, response);
                }
            } catch (Exception e) {
                e.printStackTrace();
                request.setAttribute("msg", "添加失败，请重新尝试");
                request.getRequestDispatcher("/admin/addGoods.jsp").forward(request, response);
            }
        } else if ("update".equals(method.trim())) {
            // 修改操作
            try {
                Goods goods = ParamsAsBean.parammeterAsBean(Goods.class, request, true);
                Part picture = request.getPart("picture");
                String newFilePath = FileUploadUtil.upload(picture, basePath);
                // 文件上传成功,返回地址
                if (newFilePath != null) {
                    goods.setPicture(newFilePath);
                }
                if (!new GoodsServiceImpl().update(basePath, goods)) {
                    request.setAttribute("msg", "商品修改失败,请重新尝试");
                    request.getRequestDispatcher("/goodsservlet?method=getGoods").forward(request, response);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            response.sendRedirect(request.getContextPath() + "/goodsservlet?method=getGoods");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

}
